package com.longcai.controller.homepage;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.homepage.GoodsClass;
import com.longcai.service.homepage.GoodsClassService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品类型 前端控制器
 * </p>
 *
 * @author LWK
 * @since 2019-03-20
 */
@Controller
@RequestMapping("/goodsClass")
public class GoodsClassController {
    @Autowired
    GoodsClassService bannerService;
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * description: 首页活动分类展示
     * author: LWK
     * time：2019年3月21日10点58分
     */
    @RequestMapping("/getHomepageClassList")
    @ResponseBody
    public ApiResult<List<GoodsClass>> getHomepageClassList(@RequestBody JSONObject params) {
        ApiResult<List<GoodsClass>> result = new ApiResult<List<GoodsClass>>();
        try {
            QueryWrapper<GoodsClass> queryWrapper = new QueryWrapper<GoodsClass>();
            if (params.getString("type") != null && params.getString("type") != "") {
                queryWrapper.eq("type", params.getString("type"));
            }
            if (params.getString("type_status") != null && params.getString("type_status") != "") {
                queryWrapper.eq("type_status", params.getString("type_status"));
            }
            queryWrapper.orderByAsc("type_sort");
            result.success();
            result.setData(bannerService.selectList(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode("-1");
            result.fail("查询失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * description: 一级分类展示
     * author: LWK
     * time： 2019年3月22日09点42分
     */
    @RequestMapping("/getGoodsClassList")
    @ResponseBody
    public ApiResult<List<GoodsClass>> getGoodsClassList(HttpServletRequest request) {
        ApiResult<List<GoodsClass>> result = new ApiResult<List<GoodsClass>>();
        try {
            QueryWrapper<GoodsClass> queryWrapper = new QueryWrapper<GoodsClass>();
            //分类类型（0：活动分类；1：商品分类）
            queryWrapper.eq("type", "2");
            //分类状态（0：正常；1：禁用；2：删除）
            queryWrapper.eq("type_status", "0");
            //判断为一级分类
            queryWrapper.isNull("p_id");
            //排序字段
            queryWrapper.orderByAsc("type_sort");
            result.success();
            result.setData(bannerService.selectList(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode("-1");
            result.fail("查询失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * description: 根据一级分类获取品牌以及所有分类
     * author: LWK
     * time： 2019年3月22日10点10分
     */
    @RequestMapping("/getALLClassList")
    @ResponseBody
    public Map<String, Object> getALLClassList(@RequestBody JSONObject params) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            result = bannerService.getAllClassList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 热销商品
     *
     * @param params
     * @return
     */
    @RequestMapping("/getTopgoods")
    @ResponseBody
    public ApiResult getTopgoods(@RequestBody JSONObject params) {
        //热销排行id
        String id = params.getString("id");

        if (id == null || id.equals("")) {
            logger.error("查询失败");
            return ApiResult.fail("查询失败");
        }
        return this.bannerService.getTopgoods(id, params);
    }

    /**
     * 获取首页商品的所有二级分类
     *
     * @return
     */
    @RequestMapping("/getgoodsclass")
    @ResponseBody
    public ApiResult getgoodsclass() {

        return this.bannerService.getgoodsclass();
    }

    /**
     * 好物推荐
     *
     * @param params
     * @return
     */
    @RequestMapping("/getrecommendgoods")
    @ResponseBody
    public ApiResult getrecommendgoods(@RequestBody JSONObject params) {
        //热销排行id
        String id = params.getString("id");

        if (id == null || id.equals("")) {
            logger.error("查询失败");
            return ApiResult.fail("查询失败");
        }
        return this.bannerService.getrecommendgoods(id);
    }

    /**
     * 附近全部分类
     *
     * @return
     */
    @RequestMapping("/getNearMenuList")
    @ResponseBody
    public Map<String, Object> getNearMenuList() {
        Map<String, Object> result = new HashMap();
        result = this.bannerService.getNearMenuList();
        return result;
    }
}